superstrict
Framework brl.GLMax2D
import brl.standardio
import brl.pngloader
import brl.pixmap

global xres# = 640
global yres# = 640

SetGraphicsDriver GLMax2DDriver()

glgraphics xres,yres,0,0,2
Global texHandle:Int = GLTexFromPixmap( LoadPixmapPng("texture.png") )

glDisable GL_TEXTURE_2D
glMatrixMode GL_PROJECTION
glLoadIdentity
glOrtho 0,xres,yres,0,-1,1
glMatrixMode GL_MODELVIEW
glLoadIdentity
glViewport 0,0,xres,yres

global width:int = 128
global height:int = 128
global data:byte ptr = memalloc(width*height*4) 'size of byte always 1
global tex:int = LoadTextureMEM(data,width,height,false)

local sec:int = 0

while not keyhit(KEY_ESCAPE)
	glClear(GL_COLOR_BUFFER_BIT)
	
	
	glColor3f(1,1,1)
	glBegin(GL_LINES)
		glVertex2f(0,0)
		glVertex2f(320,240)
	glEnd()
	
	local timer1:int = millisecs()
	draw()
	local timer2:int = millisecs()
	
	glColor3f 0,0,0

	local tim:int = millisecs()
	if sec < tim
		sec = tim +1000
		print "msec: "+(timer2-timer1)
	endif

	glDisable(GL_TEXTURE_2D)

	flip
wend


function draw()
	local tim:int = millisecs() / 2
	
	for local i:int = 0 until width
	for local j:int = 0 until height
		local idx:int = 4*(i*width+j)
		local cx:int = i-width/2
		local cy:int = j-height/2
		local col:int = floor( (i+cx) ~ (j+cy) )
		data[idx+0] = 255
		data[idx+1] = col
		data[idx+2] = col
		data[idx+3] = 255
	next
	next
	
	LoadTextureMEM(data,width,height,false)
	glEnable(GL_TEXTURE_2D)

	glBindTexture(GL_TEXTURE_2D, texHandle )

	'glDisable(GL_BLEND);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_ALPHA)

	glColor4f(1,1,1,1)
	local siz:float = 150
	glBegin(GL_QUADS)
		glTexcoord2d(0,0); glVertex2f(0,0)
		glTexcoord2d(1,0); glVertex2f(xres,0)
		glTexcoord2d(1,1); glVertex2f(xres,yres)
		glTexcoord2d(0,1); glVertex2f(0,yres)
	glEnd()
	
	glDisable(GL_ALPHA_TEST);
	
	'glBlendEquation(GL_FUNC_ADD)
	
	'glBlendFunc (GL_ONE, GL_ONE);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE)
	'glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_ALPHA)
	
	glColor4f(1,1,1, float(mousex()) / float(graphicswidth()) )
	
	glBindTexture(GL_TEXTURE_2D,tex)
	glBegin(GL_QUADS)
		glTexcoord2d(0,0); glVertex2f(0,0)
		glTexcoord2d(1,0); glVertex2f(xres,0)
		glTexcoord2d(1,1); glVertex2f(xres,yres)
		glTexcoord2d(0,1); glVertex2f(0,yres)
	glEnd()
	
	glDeleteTextures(1,varptr tex)
	glDisable(GL_TEXTURE_2D)
end function


' load tex from memory
function LoadTextureMEM:int( data:byte ptr, width:int, height:int, wrap:int )
	local texture:int;
	glGenTextures( 1, varptr texture ); ' allocate a texture name
	glBindTexture( GL_TEXTURE_2D, texture ); ' select our current texture
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );' when texture area is small    
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); ' when texture area is large. Try to swap GL_NEAREST with GL_LINEAR
	' if wrap is true, the texture wraps over at the edges (repeat)  false, the texture ends at the edges (clamp)
	
	local wrapMode :int
	if wrap then wrapMode = GL_REPEAT else wrapMode = GL_CLAMP 
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode ); 
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode );
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); ' select modulate to mix texture with color for shading
	'glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );     
	' target, level-of-detail, internal format, width, height, border, format, type of pixeldata, data void ptr   
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
	return texture;
end function
